<template>
  <div class="login-page">
    <div class="login-container">
      <div class="login-left">
        <div class="welcome-text">
          <h1>欢迎回来</h1>
          <p>请登录您的账户以继续</p>
        </div>
      </div>
      
      <div class="login-right">
        <div class="login-card">
          <div class="login-header">
            <h2>用户登录</h2>
            <p>使用您的账户信息登录系统</p>
          </div>
          
          <el-form 
            :model="loginForm" 
            :rules="rules" 
            ref="loginFormRef" 
            label-position="top"
            class="login-form"
          >
            <el-form-item label="用户名" prop="username">
              <el-input 
                v-model="loginForm.username" 
                placeholder="请输入用户名"
                prefix-icon="User"
                size="large"
              />
            </el-form-item>
            
            <el-form-item label="密码" prop="password">
              <el-input 
                type="password" 
                v-model="loginForm.password" 
                placeholder="请输入密码"
                show-password
                prefix-icon="Lock"
                size="large"
              />
            </el-form-item>
            
            <el-button 
              type="primary" 
              size="large" 
              round 
              @click="submitForm"
              class="login-btn"
            >
              登 录
            </el-button>
            
            <div class="login-footer">
              <p>还没有账户? <el-link type="primary" @click="$router.push('/register')">立即注册</el-link></p>
            </div>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { ElMessage } from 'element-plus';
import http from '@/utils/request'
import { encryptPassword } from '@/utils/crypto';
import { setToken } from '@/utils/auth';

const loginFormRef = ref(null);
const loginForm = ref({
  username: '',
  password: ''
});

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 16, message: '长度在3到16个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 18, message: '长度在6到18个字符', trigger: 'blur' }
  ]
};

const submitForm = () => {
  loginFormRef.value.validate((valid) => {
    if (valid) {
      let data = {
        username: loginForm.value.username,
        password: encryptPassword(loginForm.value.password)
      };
      http.post('/system_user/login', data).then(res => {
          ElMessage.success('登录成功!');
          setToken(res.data.token);
          // $router.push('/dashboard');
      })
    } else {
      return false;
    }
  });
};
</script>

<style scoped>
.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  padding: 20px;
}

.login-container {
  display: flex;
  width: 900px;
  background: white;
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.login-left {
  flex: 1;
  padding: 60px;
  background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
  color: white;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.welcome-text h1 {
  font-size: 32px;
  margin-bottom: 16px;
  font-weight: 600;
}

.welcome-text p {
  font-size: 16px;
  opacity: 0.9;
}

.login-right {
  flex: 1;
  padding: 60px;
  display: flex;
  align-items: center;
}

.login-card {
  width: 100%;
}

.login-header {
  margin-bottom: 40px;
  text-align: center;
}

.login-header h2 {
  font-size: 28px;
  color: #303133;
  margin-bottom: 8px;
  font-weight: 600;
}

.login-header p {
  color: #909399;
  font-size: 14px;
}

.login-form :deep(.el-form-item__label) {
  font-weight: 500;
  color: #606266;
  margin-bottom: 8px;
}

.login-btn {
  width: 100%;
  margin-top: 10px;
  font-size: 16px;
  letter-spacing: 2px;
}

.login-footer {
  margin-top: 24px;
  text-align: center;
  color: #909399;
  font-size: 14px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-container {
    flex-direction: column;
    width: 100%;
  }
  
  .login-left {
    padding: 40px 20px;
  }
  
  .login-right {
    padding: 40px 20px;
  }
}
</style>
